using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	/// <summary>
	/// Summary description for LinkButton.
	/// </summary>
	public class LinkButton: System.Web.UI.WebControls.LinkButton, IContent, IConfigurationAware, ILabelledControl, IControlDataLoadAware
	{
		private ICommandConfiguration configuration;
		private string configurationId = "configuration.default.LinkButton";
		private string textKey;
		protected IDataBinder binder;
		

		private String contentPlaceHolderId;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public virtual IConfiguration Configuration
		{
			get { return configuration; }
			set { this.configuration = (ICommandConfiguration) value; }
		}

		public string ConfigurationId
		{
			get { return configurationId; }
			set { this.configurationId = value; }
		}

		public virtual string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public LinkButton()
		{
		}

		public LinkButton(IConfiguration configuration)
		{
			this.Configuration = configuration;
			this.ConfigurationId = configuration.ObjectName;
//			this.ID=configuration.Id;
			BuildControlFromConfiguration();
		}

		public LinkButton(string id, string textKey, string text, IConfiguration configuration ):this(configuration)
		{
			this.ID = id;
			this.TextKey = textKey;
			this.Text = text;
		}

		public LinkButton(string id, string textKey, string text)
		{
			this.ID = id;
			this.TextKey = textKey;
			this.Text = text;
		}


		protected override void OnInit(EventArgs e)
		{
			if(configuration == null)
			{
				configuration = (ICommandConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
			}
			this.Click += new EventHandler(this.OnClick);
            ControlUtil.SetupClientAttributes(this, configuration);
            if (configuration.CssClass != null)
            {
                this.CssClass = configuration.CssClass;
            }
			//Build();			
			binder=new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
			base.OnInit (e);
		}




/*
		protected void Build()
		{
			this.Visible = configuration.IsVisibleForPrincipal();
			this.Enabled = configuration.IsEnabledForPrincipal();
//			BuildControlFromConfiguration();
		}

*/

		protected virtual void BuildControlFromConfiguration()
		{
			this.ID=configuration.Id;
			this.TextKey = configuration.TextKey;
			this.CausesValidation = configuration.CausesValidation;
			this.Visible = configuration.IsVisibleForPrincipal(); //&&configuration.Visible;
			this.Enabled = configuration.IsEnabledForPrincipal()&&configuration.Enabled;
		}

		protected void OnClick(object sender, EventArgs e)
		{
			if (CausesValidation && !Page.IsValid)
			{
				return;
			}
			System.Web.UI.WebControls.LinkButton button = (System.Web.UI.WebControls.LinkButton)sender;
			IExecutionContext executionContext = ExecutionContext.GetCurrent(ClientID);
			executionContext["infrastructure.commandName"] = button.CommandName;
			executionContext["infrastructure.commandArgument"] = button.CommandArgument;
			ControlUtil.ExecuteAction(executionContext, Context, Configuration, this.ClientID);
			RaiseBubbleEvent(this, new ModelUpdateEventArgs(executionContext));
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			Unbind();
			ControlUtil.ExecuteOnLoad(executionContext, Context, Configuration, this.ClientID);
		}

		public void OnDataLoad()
		{
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnDataLoad(executionContext, Context, Configuration, this.ClientID);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnPreRender(executionContext,Context, Configuration, this.ClientID);
			Bind();
			this.Text = configuration.GetLocalizedPostfixLabel(TextKey, this.Text);

		}



		private void Bind()
		{
			binder.Bind(new string[]{"Text", "TextKey", "Visible", "Enabled"});
			Visible=Visible&&configuration.IsVisibleForPrincipal();
			Enabled=Enabled&&configuration.IsEnabledForPrincipal();

            if (configuration.CommandArgumentGenerator!=null)
            {
			    //TODO: refactor
			    this.CommandName = configuration.CommandName;
			    this.CommandArgument = configuration.CommandArgumentGenerator.GenerateCommandArguments(configuration, ExecutionContext.GetCurrent(ClientID));			                    
            }

            AssignJScriptCommand();
		}

		private void Unbind()
		{
			binder.Unbind(new string[]{"Text", "TextKey", "Visible", "Enabled"});	
		}
		

		//TODO: refactor common Button logic
		private void AssignJScriptCommand()
		{
			if (configuration.JScriptCommandName != null && Enabled)
            {
                if (configuration.CommandArgumentGenerator != null)
                {
                    Attributes.Add("onclick", configuration.JScriptCommandName + "('" + configuration.CommandArgumentGenerator.GenerateCommandArguments(configuration, ExecutionContext.GetCurrent(ClientID)) + "');");
                }
                else
                {
                    Attributes.Add("onclick", String.Format("{0}();", configuration.JScriptCommandName));
                }
            }
		}


	}
}
